package com.panyunting.notepad.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.panyunting.notepad.javabean.Note;

import java.util.ArrayList;
import java.util.List;

/**
 * 这个类专门用于数据库的增删改查
 */
public class NotepadDao {
    private SQLiteDatabase db;
    //当NotepadDao这个类被创建的时候，构造方法就会执行，那么数据库就会打开
    public NotepadDao(Context context){
        MyOpenHelper openHelper = new MyOpenHelper(context);
        db = openHelper.getReadableDatabase();
    }

    /**
     * 完成从数据库中读取数据的功能
     * @return 返回数据源
     */
    public List<Note> queryData() {
        String sql = "select * from note";
        Cursor cursor = db.rawQuery(sql, null);
        ArrayList<Note> noteArrayList = new ArrayList<>();
        while (cursor.moveToNext()) {
            //读取一条数据，分别获得三个字段值
            String id = cursor.getString(0);
            String content = cursor.getString(1);
            String noteTime = cursor.getString(2);
            String name = cursor.getString(2);
            //创建好一个note对象
//            Note note = new Note(id, content, noteTime,);
            //装到list集合中
//            noteArrayList.add(note);
        }

        return  noteArrayList;
    }
    //这里的？代表占位符，需要通过参数去传递
    public void deleteById(String id) {
        String sql = "delete from note where id=?;";
        db.execSQL(sql,new String[]{id});
    }
    //在数据库Dao中创建插入数据的方法
    public void insertData(String content, String time) {
        String sql = "insert into note(content,notetime) values(?,?);";
        db.execSQL(sql,new String[]{content,time});
    }

    public void upateData(String id,String content,String notetime) {
        String sql = "update note set content = ?,notetime = ? where id = ?;";
        db.execSQL(sql,new String[]{content,notetime,id});
    }
}
